<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'ajax1.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

<script type="text/javascript">
//创建异步对象
function  createXMLHttpRequest(){
	try {
		return new XMLHttpRequest();//支持大多数浏览器
	} catch (e) {
		try {
			return  ActvieXObject("Msxml2.XMLHTTP");//IE6.0
		} catch (e) {
			try {
				return ActvieXObject("Microsoft.XMLHTTP")//IE5.0
			} catch (e) {
				// TODO: handle exception
				alter("浏览器不支持异步操作");
				throw e;
			}
		}
	}
}
//文档加载完成后执行
window.onload=function(){
	var btn=document.getElementById("btn");
	btn.onclick=function(){///给按钮注册监听事件
		
		/*
		ajax四步操作
		1.得到异步对象
		2.打开与服务器的连接
		3.发送请求
		4.给异步对象onreadystatechang事件注册监听器
		
		
		*/
		
		var XmlHttp=createXMLHttpRequest();
		XmlHttp.open("POST","<c:url value='/AServlet'/>",true);
		XmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		XmlHttp.send("username=张三&password=123123");
		XmlHttp.onreadystatechange=function(){
			if(XmlHttp.readyState==4 && XmlHttp.status==200){
				var text=XmlHttp.responseText;
				var h1=document.getElementById("h1");
				h1.innerHTML=text;
				
			}
		};
	};
}


</script>

  </head>
  
  <body>
    This is my JSP page. <br>
    <button id="btn">点击</button>
    <h1 id="h1"></h1>
  </body>
</html>
